package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.service.Impl.CategoryServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by dell on 2017/11/18.
 */
public class CategoryController {
    CategoryService service=new CategoryServiceImpl();
    @URLMapping(url = "/findAllCategory")
    public String findAllCategory(HttpServletRequest request, HttpServletResponse response)
    {
        List<Category> list=service.findAllCategory();
        request.setAttribute("list",list);
        return "/admin/category/list.jsp";
    }

    @URLMapping(url = "/categoryAddUI")
    public String categoryAddUI(HttpServletRequest request, HttpServletResponse response)
    {
        return"/admin/category/add.jsp";
    }

    @URLMapping(url = "/categoryAdd")
    public String categoryAdd(HttpServletRequest request, HttpServletResponse response)
    {
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            service.categoryAdd(category);
            return "/findAllCategory";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @URLMapping(url = "/CategoryEditUI")
    public String CategoryEditUI(HttpServletRequest request, HttpServletResponse response)
    {
        String cid=request.getParameter("cid");
        Category category=service.findCategoryById(cid);
        request.setAttribute("category",category);
        return "/admin/category/edit.jsp";
    }

    @URLMapping(url = "/CategoryEdit")
    public String CategoryEdit(HttpServletRequest request, HttpServletResponse response)
    {
        Category category=new Category();
        try {
            BeanUtils.populate(category,request.getParameterMap());
            service.categoryEdit(category);
            return "/findAllCategory";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @URLMapping(url = "/CategoryDelete")
    public String CategoryDelete(HttpServletRequest request, HttpServletResponse response)
    {
        String cid=request.getParameter("cid");
        service.CategoryDelete(cid);
        return "/findAllCategory";
    }
}
